package com.crmweb.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.crmweb.entity.DynamicmMessages;
import com.crmweb.entity.Result;
import com.crmweb.entity.Roles;
import com.crmweb.entity.Users;
import com.crmweb.service.DynamicmMessagesService;
import com.crmweb.service.UsersService;
import com.crmweb.tool.ConstantUtil;

@RestController
public class DynamicmMessagesController {

	@Autowired
	private DynamicmMessagesService dms;
	@Autowired
	private UsersService us;
	
	@RequestMapping("netFile/addDynamicmMessages")
	public Object addDynamicmMessages(DynamicmMessages dm) {
		int i = dms.addDynamicmMessages(dm);
		if(i>0) {
			Result res = new Result("添加动态消息成功","",true);
			return res;
		}else {
			Result res = new Result("添加动态消息失败","",false);
			return res;
		}
		
	}
	//咨询师角色登录查看有无未读消息
	@RequestMapping("getCountDMByAskerId")
	public Object getCountDMByAskerId(String rid,HttpServletRequest request) {
		
		HttpSession session=request.getSession(); 
		Users users=(Users)session.getAttribute(ConstantUtil.USERS);
		String userId = users.getId();
		List<String> ridlist = us.selRoleIdByUid(userId);
		List<Roles> rlist = new ArrayList<Roles>();
		for (String string : ridlist) {
			Roles rname = us.selRoleByRoleId(string);
			rlist.add(rname);
		}
		List<String> rnamelist = new ArrayList<>();
		for (Roles roles : rlist) {
			String rname = roles.getName();
			rnamelist.add(rname);
		}
		Result res = new Result();
		if(rnamelist!=null &&  rnamelist.contains("咨询师")){
		
			int i = dms.getCountDMByAskerId();
			res = new Result(i,"",true);
			return res;
			
		}else {
			res = new Result("","",false);
			return res;
		}
		
		
	}
	//查看当前咨询师的数据
	@RequestMapping("sysM/selAllByAskId")
	public Map<String, Object> selAllByAskId(String isOpen,HttpServletRequest request){
		
		List<DynamicmMessages> dmlist = dms.selAllByAskId(isOpen);
		Map<String, Object> map = new HashMap<>();
		map.put("code", 0);
		map.put("data", dmlist);
		map.put("count", dms.getcountBuWhere(isOpen));
		return map;
		
	}
	@RequestMapping("sysM/updDMYD")
	public Object updDMYD(int id) {
		System.out.println("-------------------"+id);
		int i = dms.updDMYD(id);
		if(i>0) {
			Result res = new Result("","",true);
			return res;
		}else {
			Result res = new Result("","",false);
			return res;
		}
		
	}
	@RequestMapping("sysM/delDMYD")
	public Object delDMYD(int id) {
		int i = dms.delDMYD(id);
		System.out.println("-------------------"+id);
		if(i>0) {
			Result res = new Result("删除成功","",true);
			return res;
		}else {
			Result res = new Result("删除失败","",false);
			return res;
		}
		
	}
	
}
